package com.retirement.enroll.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retirement.activity.domain.OldActivity;
import com.retirement.activity.mapper.OldActivityMapper;
import com.retirement.enroll.vo.EnrollVo;
import com.retirement.information.domain.OldInformation;
import com.retirement.information.mapper.OldInformationMapper;
import com.retirement.common.utils.bean.BeanUtils;
import com.retirement.enroll.domain.OldEnroll;
import com.retirement.enroll.mapper.OldEnrollMapper;
import com.retirement.enroll.service.IOldEnrollService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2024-01-02
 */
@Service
public class OldEnrollServiceImpl extends ServiceImpl<OldEnrollMapper, OldEnroll> implements IOldEnrollService {

    @Resource
    private OldActivityMapper oldActivityMapper;

    @Resource
    private OldInformationMapper oldInformationMapper;

    @Override
    public int addEnroll(OldEnroll dto) {
        OldActivity oldActivity = oldActivityMapper.selectById(dto.getActivityId());
        Assert.isTrue(ObjectUtil.isNotNull(oldActivity), "无活动信息");
        Assert.isTrue(!oldActivity.getState().equals("5"), "活动已结束");
        OldInformation oldInformation = oldInformationMapper.selectById(dto.getInformationId());
        Assert.isTrue(ObjectUtil.isNotNull(oldInformation), "无报名人信息");
        dto.setId(IdUtil.simpleUUID());

        return baseMapper.insert(dto);
    }

    @Override
    public List<EnrollVo> listEnroll(String id) {
        List<EnrollVo> enrollVos = new ArrayList<>();
        List<OldEnroll> oldEnrolls = baseMapper.selectList(new LambdaQueryWrapper<OldEnroll>()
                .eq(OldEnroll::getInformationId,id));
        Assert.isTrue(ObjectUtil.isNotNull(oldEnrolls), "无活动信息");
        oldEnrolls.forEach(e->{
            OldActivity bean = oldActivityMapper.selectById(e.getActivityId());
            if(ObjectUtil.isNotNull(bean)){
                EnrollVo enrollVo = new EnrollVo();
                BeanUtils.copyProperties(bean,enrollVo);
                enrollVos.add(enrollVo);
            }
        });
        return enrollVos;
    }


}
